package com.gs.cd.service.resource.config;

import cn.hutool.core.util.StrUtil;
import com.gs.cd.service.resource.api.ResourceApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(GsFrameworkResource.class)
public class AutoResourceConfiguration {

    @Autowired
    protected GsFrameworkResource gsFrameworkResource;

    @Bean
    @ConditionalOnMissingBean(ResourceApi.class)
    public ResourceApi resourceApi() {
        String url = gsFrameworkResource.getUrl();
        String saleCode = gsFrameworkResource.getSaleCode();
        if (StrUtil.isEmpty(url)) {
            throw new RuntimeException("初始化资源服务错误，请服务地址配置：gs.framework.resource.url");
        }
        if (StrUtil.isEmpty(saleCode)) {
            throw new RuntimeException("初始化资源服务错误，请当前销售单元saleCode:gs.framework.resource.saleCode");
        }
        return new ResourceApi(url, saleCode);
    }
}
